package com.fc.advbox.job;

import com.fc.advbox.entity.InvestorAcctEntity;
import com.fc.advbox.entity.InvestorAssetEntity;
import com.fc.advbox.service.IInvestorAcctService;
import com.fc.advbox.service.IInvestorAssetService;
import com.fc.advbox.utils.Constant;
import com.fc.advbox.utils.DateTimeHelper;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Configurable
@EnableScheduling
public class AssetDailySettleTask {
    private static final Logger log = LoggerFactory.getLogger(AssetDailySettleTask.class);

    @Autowired
    private IInvestorAssetService iInvestorAssetService;

    //每天早晨2点8分执行一次
    @Scheduled(cron = "0 8 2 * * *")
    public void runJobByCron() {
        log.info("Scheduling Tasks AssetDailySettleTask : start at: " + new Date());
        log.info("Starting ...");
        updateAssetStatus();
        log.info("completed");
        log.info("Tasks AssetDailySettleTask: complete at: " + new Date());
    }

    /**
     * 每日清点资产状态
     */
    private void updateAssetStatus(){
        int recTotal = iInvestorAssetService.queryTotal(new HashedMap());
        for(int i = 0; i <= recTotal/100; i++){
            List<InvestorAssetEntity> assetList = iInvestorAssetService.queryLimit(i*100,100);
            for(InvestorAssetEntity asset: assetList){
                if(asset.getEndTime().before(new Date()) &&
                        Constant.AssetStatus.ACTIVE.getValue().equals(asset.getStatus())){
                    log.info("Asset id:["+asset.getId()+"] is due");
                    asset.setStatus(Constant.AssetStatus.DUE.getValue());
                    iInvestorAssetService.update(asset);
                }
            }
        }
    }
}